package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.afd.ozblip.activity.AddVipActivity;
import com.afd.ozblip.activity.R;
import com.afd.ozblip.activity.VipDetailActivity;
import com.afd.ozblip.entity.VipEntity;
import com.afd.ozblip.helper.VipVH;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.StringUtil;

public class VipListAdapter extends BaseAdapter {

	public List<VipEntity> dataList;
	private LayoutInflater inflater;
	private Context context;

	public VipListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<VipEntity>();
		this.inflater = LayoutInflater.from(context);
	}

	/**
	 * 获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			VipVH vipVH = null;
			if (paramView == null) {
				vipVH = new VipVH();
				paramView = this.inflater.inflate(
						R.layout.nearby_business_vip_item, null);

				vipVH.businessNameTv = (TextView) paramView
						.findViewById(R.id.tv_vip_list_item_businessName);
				vipVH.businessAreaTv = (TextView) paramView
						.findViewById(R.id.tv_vip_list_item_area);
				vipVH.vipTitleTv = (TextView) paramView
						.findViewById(R.id.tv_vip_list_item_title);
				vipVH.businessDistanceTv = (TextView) paramView
						.findViewById(R.id.tv_vip_list_item_distance);
				vipVH.toAddBtn = (Button) paramView
						.findViewById(R.id.btn_vip_list_item_add);
				paramView.setTag(vipVH);
			} else {
				vipVH = (VipVH) paramView.getTag();
			}

			final VipEntity vip = this.dataList.get(position);
			if (vip != null) {
				// 商家名称
				String businessName = vip.getBusinessName();
				if (!StringUtil.isNullOrEmpty(businessName)) {
					vipVH.businessNameTv.setText(businessName);
				}
				// 商家地区
				String businessArea = vip.getBusinessArea();
				if (!StringUtil.isNullOrEmpty(businessArea)) {
					vipVH.businessAreaTv.setText(businessArea);
				}
				// 会员卡标题
				String vipTitle = vip.getVipTitle();
				if (!StringUtil.isNullOrEmpty(vipTitle)) {
					vipVH.vipTitleTv.setText(vipTitle);
				}
				// 距离商家距离
				String businessDistance = vip.getBusinessDistance();
				if (!StringUtil.isNullOrEmpty(businessDistance)) {
					vipVH.businessDistanceTv.setText(businessDistance);
				}
				vipVH.toAddBtn.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent settingIntent = new Intent(context,
								AddVipActivity.class);
						settingIntent.putExtra("businessId", vip.getBusinessId());
						settingIntent.putExtra("vipId", vip.getVipId());
						context.startActivity(settingIntent);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<VipEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}
}
